package com.leyou.item.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.pojo.TbSpu;
import com.leyou.item.service.TbSkuService;
import com.leyou.item.service.TbSpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: guojy
 * @date: 2020/3/4 18:38
 * @Description:
 * @version: 1.0
 */
// http://api.leyou.com/api/item/spu/page?key=&saleable=true&page=1&rows=5
@RestController
public class GoodsController {

    @Autowired
    private TbSpuService tbSpuService;

    // TODO 商品信分页查询
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuDTO>> findSpuByPage1(
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "rows", defaultValue = "5") Integer rows,
            @RequestParam(name = "key", required = false) String key,
            @RequestParam(name = "saleable", required = false) Boolean saleable) {
        PageResult<SpuDTO> pageResult = tbSpuService.findSpuByPage(page, rows, key, saleable);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 新增商品
     *
     * @param spuDTO 页面提交商品信息
     * @return
     */
    @PostMapping("/goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO) {
        tbSpuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改商品
     *
     * @param spuDTO 页面提交商品信息
     * @return
     */
    @PutMapping("/goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        tbSpuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * http://api.leyou.com/api/item/spu/saleable put
     * 更改商品 上架状态
     *
     * @param spuID    spuid
     * @param saleable 是否上架下架标识
     */
    @PutMapping("/spu/saleable")
    public ResponseEntity<Void> updateSpuSaleable(
            @RequestParam(name = "id") Long spuID,
            @RequestParam(name = "saleable") Boolean saleable) {
        tbSpuService.updateSpuSaleable(spuID, saleable);
        // 保存成功 没有特定的 状态码
        return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
    }

    /**
     * 根据spuid  查询spuDetail
     *
     * @param id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> findSpuDetailBySpuId(Long id) {
        SpuDetailDTO spuDetailDTO = tbSpuService.findSpuDetailBySpuId(id);
        return ResponseEntity.ok(spuDetailDTO);
    }

    @Autowired
    private TbSkuService tbSkuService;

    /**
     * 根据 spuid  查所有的sku
     *
     * @param spuId spuid
     */
    @GetMapping("sku/of/spu")
    public ResponseEntity<List<SkuDTO>> findSkusBySpuId(@RequestParam(name = "id") Long spuId) {
        return ResponseEntity.ok(tbSkuService.findSkusBySpuId(spuId));

    }

    /**
     * 根据 id 查询寻spu
     *
     * @param id
     * @return
     */
    @GetMapping("spu/{id}")
    public ResponseEntity<SpuDTO> findSpuById( @PathVariable("id") Long id) {
        SpuDTO spuDTO = tbSpuService.findSpuById(id);
        return ResponseEntity.ok(spuDTO);
    }

}
